home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12016 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  106 lines

  1. Path: argonet.co.uk!argbe15
  2. From: Dave Mullard <dmullard@argonet.co.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to get at base class fields from member functions?
  5. Date: Sun, 17 Mar 1996 20:03:46
  6. Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX
  7. Distribution: world
  8. Message-ID: <internews46B0766104@argonet.co.uk>
  9. References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com>
  10. Reply-To: Dave Mullard <dmullard@argonet.co.uk>
  11. NNTP-Posting-Host: ai213.du.pipex.com
  12. X-Newsreader: VTi Voyager InterNews 0.15 for Acorn RISC OS (Patched by RA)
  13.  
  14. Chris Cobb <ccobb@erols.com> wrote:
  15. > I <dmullard@argonet.co.uk> wrote:
  16. > >Given three classes A, B and C I want to have multiple As for each B
  17. > and
  18. > >multiple Bs for each C. To do this I could create the following.
  19. > >
  20. > >class b1 : public B
  21. > >{
  22. > >A a1;
  23. > >A a2;
  24. > >A a3;
  25. > >};
  26.  
  27. > >class b2 : public B
  28. > >{
  29. > >A a1;
  30. > >A a2;
  31. > >A a3;
  32. > >A a4;
  33. > >A a5;
  34. > >};
  35.  
  36. > >class c1 : public C
  37. > >{
  38. > >b1 b1;
  39. > >b2 b2;
  40. > >};
  41.  
  42. > >The question is, how within functions for class B do I access fields
  43. > >within class C, and similarly, how within the class A functions do I
  44. > >access fields within class B?
  45.  
  46.  
  47. > You have encountered the wall of encapsulation.  It is considered poor 
  48. > form to violate this wall without good reason.  The need to violate this
  49.  
  50. > wall may indicate poor design.  However, C++ is not so rigid as to 
  51. > disallow this.  The way this is done is to have the class whose members 
  52. > need to be accessed declare the class who wants to access them a friend:
  53.  
  54. > class C
  55. > {
  56. >    friend class B;
  57. >    // ...
  58. > };
  59.  
  60. > class B
  61. > {
  62. >    friend class A;
  63. >    // ...
  64. > };
  65.  
  66. I obviously have not explained myself very well.
  67.  
  68. Maybe instead of *access* I should have said identify.
  69.  
  70. If, for example, B is
  71.  
  72. class B
  73. {
  74. friend class A; 
  75. int fred;
  76. }
  77.  
  78. I cannot just
  79.  
  80. A::A() {fred=21;};
  81.  
  82. or even 
  83.  
  84. A::A() {B::fred=21;};
  85.  
  86. The As are only members of a class that has B as a base.
  87.  
  88. I suppose at its simplest level the problem is this
  89.  
  90. class B
  91. {
  92. int fred;
  93.    class A
  94.    {
  95.       A() { };
  96.    };
  97. A a1;
  98. A a2;
  99. };
  100.  
  101. How within A::A do I refer to fred. 
  102.  
  103. -- 
  104.  Dave Mullard <dmullard@argonet.co.uk>
  105.  
  106.